/*
 * HWaccess.h
 *
 *  Created on: 11.04.2011
 *      Author: Jasper
 */

#ifndef HWACCESS_H_
#define HWACCESS_H_

#include <pthread.h>
#include <hw/inout.h>
#include <stdio.h>

#include "constants.h"

#ifdef SIMULATION
#include <ioaccess.h>
#endif


/**
 * This class is for easy and threadsave HW-acess to the HAW-Factory-Assambly
 * It contains accessfunctions for the HW.
 * Also constants for these functions are given.
 */
class HWaccess {

public:

    /**
     * Initializes HW.
     * returns 0 if successful.
     */
    static int init();

    /**
     * Sets given bit at given port to val.
     * Port contains bit and port, please use defined values.
     * Val is interpreted as boolean, 0=>false, ohters=>true.
     * returns 0 if successful.
     */
    static int set(const int port, const int val);

    /**
     * Sets given bits at given ports to given values.
     * Like set(), but with multiple values and ports.
     * size has to contain the size of the arrays.
     * returns 0 if successful.
     */
    static int setV(const int* portV, const int* valV, const int size);

    /**
     * Gets the Value of the given bit and port into resu.
     * resu will contain 0 or 1 (for false or true).
     * returns 0 if successful.
     */
    static int get(const int port, int* resu);

    /**
     * Gets the Value of the given bits and ports into resuV.
     * Each element of resuV will contain 0 or 1 (for true or false).
     * size has to contain the size of the arrays.
     * returns 0 if successful.
     */
    static int getV(const int* portV, int* resuV, const int size);

protected:

    /**
     * Constructor
     */
    HWaccess();

    /**
     * Destructor
     */
    virtual ~HWaccess();

private:
    static pthread_mutex_t writeLock;
    static int portAVal;
    static int portCVal;

};

#endif /* HWACCESS_H_ */
